package com.carmanage.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.carmanage.dao.IStationDao;
import com.carmanage.domain.Station;
import com.carmanage.domain.StationInfo;



@Controller
public class StationController {
	
	@Resource
	private IStationDao stationDao;
	private List<Station> findStations;
	
	public void setStationDao(IStationDao stationDao){
		this.stationDao = stationDao;
	}
	
	public IStationDao getStationDao(){
		return stationDao;
	}
	
	
	@RequestMapping(value="/saveStation")
	public void saveStation(Station station,HttpServletResponse response){
		stationDao.save(station);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.println("success");
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.flush();
			out.close();
		}
		
	}
	
	@RequestMapping(value="/findAllStation")
	public @ResponseBody List<Station> findAllStation(){
		List<Station> stations = stationDao.findStations();
		return stations;
	}
	
	@RequestMapping(value="/getStationInfo")
	public @ResponseBody StationInfo getStationInfo(String station_name){
		System.out.println(station_name);
		StationInfo stationInfo = stationDao.findStationByName(station_name);
		return stationInfo;
	}
	
	@RequestMapping(value="/getStation")
	public @ResponseBody Station getStation(String station_name){
		Station station = stationDao.getStation(station_name);
		return station;
	}
	
	@RequestMapping(value="/delectStation")
	public String delectStation(String stationName){
		stationDao.delectByName(stationName);
		return "";
		
	}
	
	@RequestMapping(value="/updateStation")
	public void updateStation(Station station,HttpServletResponse response){
		System.out.println(station);
		stationDao.updateStation(station);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.println("success");
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.flush();
			out.close();
		}
		
	}
	
}
